package com.coins.common.jobs;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

@Component
@Configuration
public class JobCustomSchedules implements SchedulingConfigurer {

	private ScheduledTaskRegistrar taskRegistrar;

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		this.taskRegistrar = taskRegistrar;
		this.taskRegistrar.setScheduler(this.taskExecutor());
	}

	public boolean addTask(Runnable task, String expression) {
		if (task != null) {
			this.taskRegistrar.addCronTask(new CronTask(task, expression));
			return true;
		}
		return false;
	}

	@Bean(destroyMethod = "shutdown")
	public Executor taskExecutor() {
		return Executors.newScheduledThreadPool(5);
	}
}
